package io.coderyeah.ymcc.mq;

import com.alibaba.fastjson.JSON;
import io.coderyeah.ymcc.constant.MQConstants;
import io.coderyeah.ymcc.domain.MediaFile;
import io.coderyeah.ymcc.result.JSONResult;
import io.coderyeah.ymcc.service.IMediaFileService;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author zc
 * @date 2022/10/26 19:44
 */
@Component
@RocketMQMessageListener(topic = MQConstants.MQ_MEDIA_TOPIC,    //topic
        selectorExpression = MQConstants.MQ_MEDIA_TAGS,         //tags
        consumerGroup = "service-producer",                     //生产者组名字
        messageModel = MessageModel.CLUSTERING)                 //消息的消费模式 CLUSTERING：集群    BROADCASTING：广播
@Slf4j
public class MediaMQConsumer implements RocketMQListener<MessageExt> {

    @Autowired
    private IMediaFileService mediaFileService;

    @Override
    public void onMessage(MessageExt messageExt) {
        //把数据取出来消费，原始消息 - 二进制
        byte[] body = messageExt.getBody();
        //转换成字符串
        String mediaStr = new String(body);
        //转换为MediaFile
        MediaFile mediaFile = JSON.parseObject(mediaStr, MediaFile.class);
        //切片推流
        JSONResult jsonResult = mediaFileService.handleFile2m3u8(mediaFile);
        log.info("视频推送 {}",jsonResult.isSuccess());
    }
}
